package com.sy.auth.activiti;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class ProcessTest2 {
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
    @Autowired
    HistoryService historyService;

    // 1. 部署
    @Test
    public void deployProcess() {
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("process/jiaban.bpmn20.xml")
                .addClasspathResource("process/jiaban.png")
                .name("加班申请流程")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    // 2. 创建流程实例
    @Test
    public void startProcessInstance() {
        Map<String,Object> map = new HashMap<>();
        map.put("assignee1","Lucy");
        map.put("assignee2","Mary");

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban", map);
        System.out.println(processInstance.getProcessDefinitionKey());
        System.out.println(processInstance.getId());

    }

    // 3. 查询个人的待办任务
    @Test
    public void findTask() {
        Task task = taskService.createTaskQuery().taskAssignee("Lucy").singleResult();
        System.out.println(task.getAssignee());
        System.out.println(task.getProcessInstanceId());
        System.out.println(task.getName());
    }

    // 任务监听器部署
    @Test
    public void deployProcess2() {
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("process/jiaban02.bpmn20.xml")
                .name("加班申请流程02")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban02");
        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getId());

        Task task = taskService.createTaskQuery().taskAssignee("lisi").singleResult();
        System.out.println(task.getAssignee());
        System.out.println(task.getName());
        System.out.println(task.getProcessInstanceId());
    }


}
